<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/4/23 0023
 * Time: 15:39
 */
defined('BASEPATH') OR exit('No direct script access allowed');
include_once('./simple_html_dom.php');

class Youlai_ask extends CI_Controller
{
    public function urls(){
        exit;
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        // 从list拿出来一个，如果执行不成功，写到另外的list里
        $did = $redis->lpop('youlai_ask_list');
        //$did = 811;
        if(!$did){
            echo "nodata";
            exit;
        }
        $ids = $this->db->where('id',$did)->get('youlai_article_ids')->row_array();
        if(!$ids){
            echo "nodata";
            exit;
        }
//        $proxy_user = $this->db->query("select * from ai_spider_yaozh_user where id=42")->row_array();
//        $proxy_user_info = json_decode($proxy_user['proxy_info'],true);
//        $proxy['proxy'] = $proxy_user_info['proxy'];
//        $proxy['userpwd'] = $proxy_user_info['userpwd'];
        $first_url = "https://www.youlai.cn/dise/asklist/".$ids['dis_id']."_1.html";
        // 获取总页数
        //dd($proxy);
        $content = geturl($first_url);

        $html = str_get_html($content);

        if(!str_replace(" ","",$html->find('div[class=pageyl]',0)->innertext)){
//            sleep(3);
//            $redis->rPush('youlai_arc_list',$did);
            $pages = 1;
        }else{
            $pages_li = $html->find('div[class=pageyl]',0)->find('ul',0)->find('li');
            if(count($pages_li) > 0){
                // dd($pages_li);
                $pages = (int)array_reverse($pages_li)[1]->find('a',0)->innertext;
            }
        }
        for($i=1;$i<=$pages;$i++){ // 最多十页
            //https://www.youlai.cn/ask/voicelist/1_12_1_0_2.html
            $list_html = "https://www.youlai.cn/dise/asklist/".$ids['dis_id']."_".$i.".html";
            //echo $list_html."<br>";
            $content = geturl($list_html);
//            if($i < 3){
//
//                continue;
//            }
//            echo $list_html;
//            echo $content;exit;
            if(!$content){
                continue;
            }
            $html = str_get_html($content);
            if($html->find("div[id=cbox]")){
                $redis->rPush('youlai_ask_list',$did);
                continue;
            }
            $lis = $html->find("ul[class=qa_left] li");

            if(!$lis){
                continue;
            }
            $sql = "";
            $lis = array_slice($lis,0,10);
            foreach($lis as $lik=>$li){

//                echo $lik."-";
//                if($lik == 10){
//                    echo $content;
//                    dd($li->innertext);
//                }
                $did = $ids['dis_id'];
                $dept1 = $ids['dept1_name'];
                $dept2 = $ids['dept2_name'];
                $dis = str_replace('<i></i>','',$ids['dis_name']);
                $title = addslashes(trim(strip_tags($li->find("h3",0)->find("a",0)->innertext)));
                $url = trim($li->find("h3",0)->find("a",0)->href);
                $sql .= "(".$did.",'".$dept1."','".$dept2."','".$dis."','".$title."','".$url."'),";
                //echo $sql."<br>";
            }
            //echo $sql;exit;
            //echo $i;
            $sql = substr($sql,0,-1);
            $sql = 'INSERT ignore INTO ai_youlai_ask_url(did,dept1,dept2,dis,title,url) VALUES '.$sql;
            //echo $sql;exit;
//            echo "<br>";
            $this->db->query($sql);
        }

        echo $did + 1;
    }

    public function info(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        // 从list拿出来一个，如果执行不成功，写到另外的list里
        $urlid = $redis->lpop('youlai_ask_view');
        //$urlid = 237;
        if(!$urlid){
            echo "nodata";
            exit;
        }
        $url_info = $this->db->where('id',$urlid)->get('ai_youlai_ask_url')->row_array();
        if(!$url_info){
            echo "nodata";
            exit;
        }
        //echo "采集开始:".time()."-";
        $detail_url = "https://www.youlai.cn".$url_info['url'];
        $content = geturl($detail_url);
        $html = str_get_html($content);
        if($html->find("div[id=cbox]")){
            sleep(3);
            $redis->rPush('youlai_ask_view_bak',$urlid);
            echo 1;exit;
        }
        if($html->find('div[class=link404]',0)){
            echo 1;exit;
        }
        // 处理对应字段
        $department1 = $url_info['dept1'];
        $department2 = $url_info['dept2'];
        $disease = $url_info['dis'];
        $ask_name = $url_info['title'];
        $ask_content = trim($html->find("dl[class=ask01]",0)->find("dd",0)->innertext);
        $ask_content = trim(str_replace('<strong>病情描述：</strong><br />','',$ask_content));
        $pubdate = trim($html->find("dl[class=ask01]",0)->find("dt",0)->find("p",0)->find("span",0)->innertext);

        $author_title = trim($html->find("h5",0)->find("span",0)->innertext);
        $author_hospital = trim($html->find("h5",0)->find("span",1)->innertext);

        $author_name = str_replace('<span>'.$author_title.'</span><span>'.$author_hospital.'</span>','',$html->find("h5",0)->innertext);
        $answer_content = trim($html->find('div[class=docYes]',0)->innertext);
        $sql = "INSERT ignore INTO ai_spider_youlai_disease_ask(department_1,department_2,disease,ask_title,ask_content,answer_content,pubdate,doctor_name,doctor_title,doctor_hospital,url_id) VALUES ('".$department1."','".$department2."','".$disease."','".$ask_name."','".$ask_content."','".$answer_content."','".$pubdate."','".$author_name."','".$author_title."','".$author_hospital."',$urlid)";
        $this->db->query($sql);
        echo 1;
        //$ = $html->find("ul[id=videoList] li");
    }

    public function url_push(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        $ids = $this->db->query("select id from ai_youlai_ask_url")->result_array();
        foreach($ids as $id){
            $redis->rPush('youlai_ask_view',$id['id']);
        }

    }
}